<template>
  <div class="widget_settings_pro">
    <ele-form-section
        v-model="formData"
        :request-fn="handleSubmit"
        :sections="sections"
        @request-success="handleSuccess"
    >
      <template v-slot:dimension="{ desc, data, field, formData }">

        <we-collapse :list="formData.templateBlockList" accordion>
          <we-collapse-item v-for="(item, index) in formData.templateBlockList" :key="index">
            <template slot="title">
              <svg-icon icon-class="grid" style="padding-left: 10px;"></svg-icon>
              <span>{{item.type === 'color_filter' ? '颜色筛选' : item.type === 'size_filter' ?  '尺寸筛选' :  item.type === 'price_filter' ? '价格筛选' : '标签筛选'}}</span>
            </template>

            <div class="filter-content" v-if="item.type !=='price_filter'">
              <div class="title">标题</div>
              <div>
                <el-input class="input forbid" v-model="item.settings.title" placeholder="请输入"></el-input>
              </div>
              <div>
                <el-input class="input forbid" v-model.number="item.settings.tags" :placeholder="item.type === 'size_filter' ? '示例:M,X,XL' : item.type === 'color_filter' ? '示例:Red,Yellow,Blue': '示例:Tag1,Tag2,Tag3'"></el-input>
              </div>
              <div class="extra">多个标签以英文逗号隔开。</div>
              <div class="form-item">
                <el-button class="addBtn" @click="handleRemove(index)"><i class="el-icon-remove"></i>删除内容</el-button>
              </div>
            </div>

            <div class="filter-content" v-if="item.type ==='price_filter'">
              <div class="title">标题</div>
              <div>
                <el-input class="input forbid" v-model="item.settings.title" placeholder="请输入"></el-input>
              </div>
              <div class="title">价格区间的边界值</div>
              <div>
                <el-input class="input forbid" type="number" v-model.number="item.settings.minPrice" placeholder="示例:0"></el-input>
              </div>
              <div>
                <el-input class="input forbid" type="number" v-model.number="item.settings.maxPrice" placeholder="示例:150"></el-input>
              </div>
              <div class="form-item">
                <el-button class="addBtn" @click="handleRemove(index)"><i class="el-icon-remove"></i>删除内容</el-button>
              </div>
            </div>
          </we-collapse-item>
        </we-collapse>

        <el-dropdown style="margin-top: 30px;" @visible-change="handleDown" trigger="click" v-if="formData.templateBlockList.length<10">
          <el-button type="primary" icon="el-icon-add" size="small"
                     style="width: 242px;display: block;">添加内容
          </el-button>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item class="dropdownItem" @click.native="handleAddTag('tag_filter')">标签筛选</el-dropdown-item>
            <el-dropdown-item class="dropdownItem" @click.native="handleAddTag('color_filter')" :disabled="tags.color_filter">颜色筛选<span v-if="tags.color_filter">(已添加)</span></el-dropdown-item>
            <el-dropdown-item class="dropdownItem" @click.native="handleAddTag('size_filter')" :disabled="tags.size_filter">尺寸筛选<span v-if="tags.size_filter">(已添加)</span></el-dropdown-item>
            <el-dropdown-item class="dropdownItem" @click.native="handleAddTag('price_filter')" :disabled="tags.price_filter">价格筛选<span v-if="tags.price_filter">(已添加)</span></el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>

      </template>
      <template v-slot:form-btn="{ btns }">
        <div class="rest-btn" @click="reset">
          <svg-icon icon-class="reset-settings"></svg-icon>
          重置配置
        </div>
      </template>
    </ele-form-section>
  </div>
</template>

<script>
import weCollapse from '@/views/theme/components/eleCollapse/weCollapse';
import weCollapseItem from '@/views/theme/components/eleCollapse/weCollapseItem';

import {debounce, deepClone} from '@/utils/utils';
export default {
    components: {
        weCollapse,
        weCollapseItem
    },
    name: 'seriesDetailForm',
    props: {
        value: {
            type: Object,
            default: () => ({})
        }
    },
    watch: {
        value: {
            handler (newVal, oldVal) {
                this.formData = JSON.parse(newVal.styleJson);
            },
            immediate: false,
            deep: true
        },
        formData: {
            handler (newVal, oldVal) {
                this.pageCount++;
                if (this.pageCount !== 1) {
                    this.setData();
                }
            },
            immediate: false,
            deep: true
        }
    },
    created () {
        this.judgeBlockList();
    },
    data () {
        return {
            priceDisabled: false,
            pageCount: 1,
            formData: this.value ? JSON.parse(this.value.styleJson) : {},
            tags: {
                color_filter: false,
                size_filter: false,
                price_filter: false
            },
            sections: [
                {
                    title: '布局',
                    isShowLabel: true,
                    formDesc: {
                        'is_cover_show': {
                            type: 'switch',
                            break: true,
                            label: '系列封面'
                        },
                        'cover_width': {
                            type: 'select',
                            label: '系列封面宽度',
                            options: [
                                {text: '两边留白', value: 'auto'},
                                {text: '铺满全屏', value: 'full'}
                            ]
                        },
                        'name': {
                            type: 'switch',
                            break: true,
                            label: '系列名称'
                        },
                        'desc': {
                            type: 'switch',
                            break: true,
                            label: '系列描述'
                        },
                        'sort': {
                            type: 'switch',
                            break: true,
                            label: '商品排序'
                        },
                        'is_pagination_show': {
                            type: 'switch',
                            break: true,
                            label: '分页'
                        },
                        'limit': {
                            type: 'select',
                            label: '每页商品数量',
                            options: [
                                {text: '8', value: '8'},
                                {text: '12', value: '12'},
                                {text: '16', value: '16'},
                                {text: '24', value: '24'},
                                {text: '40', value: '40'},
                                {text: '48', value: '48'}

                            ]
                        },
                        'filter': {
                            type: 'switch',
                            break: true,
                            label: '商品筛选'
                        }
                    }
                },
                {
                    title: '筛选维度',
                    isShowLabel: true,
                    formDesc: {
                        'dimension': {}
                    }
                }
            ]
        };
    },
    methods: {
        handleSubmit (data) {
            return Promise.resolve();
        },
        handleSuccess () {
            this.$message.success('创建成功');
        },
        setData: debounce(function () {
            let data = deepClone(this.value);
            data.styleJson = JSON.stringify(this.formData);
            this.$store.commit('SET_PROJECT_DATA', data);
        }, 1600),
        reset () {
            this.pageCount = 0;
            this.$emit('resetData', this.value);
        },
        handleDown (e) {
            if (e) {
                for (let a in this.tags) {
                    this.tags[a] = this.judgeBlockList(a);
                }
            }
        },
        handleAddTag (name) {
            const data = [{'type': 'color_filter', 'settings': {'title': 'Color', 'tags': ''}}, {'type': 'size_filter', 'settings': {'title': 'Size', 'tags': ''}}, {'type': 'price_filter', 'settings': {'title': 'Price', 'minPrice': 0, 'maxPrice': 150}}, {'type': 'tag_filter', 'settings': {'title': '', 'tags': ''}}];
            const item = data.find((item) => item.type === name);
            this.formData.templateBlockList.push(item);
        },
        judgeBlockList (name) {
            return this.formData.templateBlockList.some((item) => item.type === name);
        },
        handleRemove (index) {
            this.formData.templateBlockList.splice(index, 1);
        }
    }
};
</script>

<style scoped lang="scss">
.el-collapse {
  width: 240px;
  box-sizing: content-box;
  .el-collapse-item {
    margin-top: 20px;
    border: 1px solid #DCDFE6;
    .el-collapse-item__header {
      span {
        padding-left: 5px;
      }
    }
    .el-collapse-item__content {
      .filter-content {
        margin: 0 10px;
        .title {
          height: 36px;
          line-height: 36px;
        }
        .links {
          text-decoration: underline;
          color: #409EFF;
        }
        .input {
          width: 216px;
        }
        .checked {
          margin: 15px 0;
          display: flex;
          flex-wrap: nowrap;
          justify-content: space-between;
        }
        .extra {
          clear: both;
          padding-top: 14px;
          color: rgba(0, 0, 0, 0.45);
          font-size: 12px;
        }
        .form-item{
          margin-top: 10px;
          height: 32px;
          line-height: 32px;
          .addBtn{
            width: 216px;
          }
        }
      }
    }
  }
}
.el-collapse-item__header {
  background: #eef0f2!important;
}
.el-form-item__content::after {
  margin-left: -100px;
}
.dropdownItem {
  width: 240px;
}
</style>
